package com.bolt.core.view.tag;

import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.ValidatorRule;

import javax.servlet.jsp.JspException;



public abstract class InputTag extends com.bolt.core.view.tag.TemplateBodyTagSupport {

	private String validate;
	private String validateMessage;

	public void setValidateMessage(String validateMessage) {
		this.validateMessage = validateMessage;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}


    public String getValidate() {
        return validate;
    }

    public String getValidateMessage() {
        return validateMessage;
    }

	@Override
	public int doEndTag() throws JspException {
        if (StrUtil.isNotBlank(validate) || StrUtil.isNotBlank(validateMessage)) {
            ValidatorRule v = handleValidator();
            if(v != null){
                component.addParameter("validator", v);
            }
        }
        super.doEndTag();
		return EVAL_PAGE;
	}



	protected ValidatorRule handleValidator() {
        ValidatorRule validator = new ValidatorRule();
		String[] vals = StrUtil.split(validate, "|");
		for (int i = 0; i < vals.length; i++) {
			validator.handleValidatorValue(vals[i]);
		}
		String[] arrymessage = StrUtil.split(validateMessage, "|");
		;
		for (int i = 0; i < arrymessage.length; i++) {
			validator.addMessage(arrymessage[i]);
		}
		validator.setId(getId());
		return validator;
	}

}